<template>
  <div class="app-container">
    <div class="processEdit">
      <div class="topbar">
        <div class="title">{{ config.configName }}</div>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
      <div class="body">
        <div class="start"><span class="txt">开始</span></div>
        <div class="list">
          <div class="item">
            <div class="box">
              <div class="title">发起</div>
              <div class="content">当前发起人</div>
            </div>
            <div class="add" @click="addNode(0)">
              <el-icon><Plus /></el-icon>
            </div>
          </div>
          <div class="item" v-for="(item, index) in list" :key="item.id">
            <div class="box" @click="toEditNode(item)">
              <div class="title">{{item.name}}</div>
              <div class="content">
                <span class="xhl-wx-work-open-data">
                  {{ showUsers(item.auditUser) }}
                </span>
              </div>

              <i aria-hidden="true" class="remove" style="font-size: 16px;" @click.stop="removeNode(item.id)">
                <el-icon><Close /></el-icon>
              </i>
            </div>
            <div class="add" @click="addNode(index + 1)">
              <el-icon><Plus /></el-icon>
            </div>
          </div>
        </div>
        <div class="end"><span class="txt">结束</span></div>
      </div>

    </div>

    <node-edit ref="editRef" :current-node="currentNode" @sub="onEditSub"></node-edit>

  </div>
</template>

<script setup>

import NodeEdit from './nodeEdit.vue';
import { getAssetAuditConfig } from "@/api/asset/assetAuditConfig.js";
import { listAssetAuditConfigNode, delAssetAuditConfigNode, updateAssetAuditConfigNode } from "@/api/asset/assetAuditConfigNode.js";

const {proxy} = getCurrentInstance();

const route = useRoute();

const editRef = ref();
const currentNode = ref({});

const config = reactive({
  id: null,
  configName: null,
});

const list = ref([]);

const emptyNode = {
  name: '普通审批',
  userId: null,
  taskType: 1,
  auditUser: []
}

function getInfo() {
  getAssetAuditConfig(config.id).then(response => {
    Object.assign(config, response);
  });
}

onMounted(()=> {
  config.id = route.params.configId;
  getInfo();
  getList();
})

function addNode(index) {
  const newNode = {...emptyNode};
  list.value.splice(index, 0, newNode);
}

function toEditNode(node) {
  currentNode.value = node;
  editRef.value.open();
}

/** 查询资产审批配置列表 */
function getList() {
  const query = {
    configId: config.id
  };
  listAssetAuditConfigNode(query).then(response => {
    if (!response) {
      list.value = [];
    }
    response.forEach(item => {
      let auditUser = item.auditUser;

      if (auditUser) {
        auditUser = JSON.parse(auditUser);
      }
      item.auditUser = auditUser;
    })

    list.value = response;
  });
}

/**
 * 删除审批节点
 * @param index
 */
function removeNode(index) {
  list.value.splice(index, 1);
}

function showUsers(users) {
  if (!users || !users.length) {
    return '请选择审批人'
  }
  return users.map(item=> item.nickName).join(',');
}

/**
 * 检查
 */
function check(list) {
  if (!list || !list.length) {
    proxy.$message.error('请填写审批节点');
    return false;
  }
  let msg = '';
  list.forEach((item, i)=> {
    if (!item.auditUser || item.auditUser.length === 0) {
      msg += '第'+(i+1)+'个节点，没有填写审批人员';
    }
  });
  if (msg) {
    proxy.$message.error(msg);
    return false;
  }
  return true;
}

/**
 * 保存
 */
function save() {
  if (!check(list.value)) {
    return;
  }

  const params = {
    list: list.value,
    configId: config.id
  }
  updateAssetAuditConfigNode(params).then(response => {
    proxy.$message.success("保存成功");
    getList();
  });
}

function onEditSub({ form, index }) {
  Object.assign(this.list[index], form);
}

</script>

<style>

.processEdit {
  height: 100%;
  .topbar {
    width: 100%;
    height: 40px;
    background-color: #fff;
    border-bottom: 1px solid #dbdcdd;
    box-sizing: border-box;
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .body {
    height: 100%;
    padding: 50px 10px 10px;
    box-sizing: border-box;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #F0F2F5;
    /** 开始节点  **/
    .start {
      width: 240px;
      height: 120px;
      display: flex;
      flex-shrink: 0;
      justify-content: center;
      position: relative;
      z-index: 5;
      &:after {
        content: "";
        width: 2px;
        height: 100%;
        background-color: #b3bfcb;
        transform: translateX(-50%);
        position: absolute;
        top: 0;
        left: 50%;
        z-index: -1;
      }
    }

    .list {
      width: 240px;
      .item {
        width: 100%;
        height: 180px;
        display: flex;
        flex-direction: column;
        align-items: center;
        position: relative;
        z-index: 5;
        &:after {
          content: "";
          width: 2px;
          height: 100%;
          background-color: #b3bfcb;
          transform: translateX(-50%);
          position: absolute;
          top: 0;
          left: 50%;
          z-index: -1;
        }

        .box {
          width: 100%;
          height: 96px;
          background-color: #fff;
          box-shadow: 0 6px 6px 0 rgba(33,43,54,.12);
          border-radius: 8px;
          box-sizing: border-box;
          padding: 10px 16px;
          cursor: pointer;
          position: relative;

          .title {
            font-size: 14px;
            font-weight: 700;
            color: #0b1019;
          }

          .content {
            height: 18px;
            line-height: 18px;
            font-size: 12px;
            color: #626262;
            margin-top: 10px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .remove {
            font-weight: 700;
            color: rgba(0,0,0,.3);
            position: absolute;
            top: 8px;
            right: 8px;
          }

        }

        .add {
          width: 24px;
          height: 24px;
          background-color: #fff;
          border-radius: 50%;
          box-shadow: 0 3px 6px rgba(0,0,0,.1);
          margin-top: 30px;
          cursor: pointer;
          display: flex;
          align-items: center;
          justify-content: center;

          &:hover {
            background-color: #005bf5;
            .el-icon {
              color: #fff;
            }
          }

        }

      }
    }

  }
}


.processEdit .body .start .txt {
    width: 50px;
    height: 50px;
    font-size: 12px;
    font-weight: 700;
    color: #005bf5;
    background-color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.processEdit .body .end {
    width: 240px;
    display: flex;
    flex-shrink: 0;
    justify-content: center;
}

.processEdit .body .end .txt {
    width: 50px;
    height: 50px;
    font-size: 12px;
    font-weight: 700;
    color: #fff;
    background-color: #005bf5;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

</style>
